<!DOCTYPE html>
<html lang='zh-CN'>

<head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="bootstrap.min.css" rel="stylesheet">
    <title>玩者荣耀-注册登录</title>
    <style>
        .game-title {
            font-size: xx-large;
        }

        .title-text {
            font-size: medium;
        }
    </style>
</head>

<body>
    <div class="modal fade" id="mymodal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1"
        aria-labelledby="staticBackdropLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="staticBackdropLabel">请输入注册码</h5>
                </div>
                <div class="modal-body">
                    <input type="text" class="form-control" id="register_code">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" data-bs-dismiss="modal">确定</button>
                </div>
            </div>
        </div>
    </div>
    <div class="container-fluid text-center">

        <h1 class="game-title">玩者荣耀-平台注册登录</h1>
       
        <input class="m-1" type="text" name="account" id="account" maxlength="20" placeholder="账号">
        <br />
        <input class="m-1" type="text" name="pwd" id="pwd" maxlength="20" placeholder="密码">
        <br />

        <button class="m-1" id="registBtn" type="submit">注册</button>
        <button class="m-1" id="loginBtn" type="submit">登录</button>
        <button class="m-1" id="updatePwdBtn" type="submit">忘记密码</button>

    </div>
</body>
<script src="jquery-3.6.0.min.js"></script>
<script src="https://cdn.staticfile.org/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script src="bootstrap.min.js"></script>
<script>
    $.ajaxSettings.timeout = '3000';
    var url = "https://2020cn.cn/hop/api/user_mgr";
    var loginBtn = document.getElementById("loginBtn");
    loginBtn.onclick = function () {
        var account = document.getElementById("account").value;
        var pwd = document.getElementById("pwd").value;
        if (account.length >= 4 && pwd.length >= 5) {
            //login
            $.post(
                url,
                {
                    "action": "loginUser",
                    "account": account,
                    "pwd": pwd
                },
                function (res) {
                    console.log(res);
                    const resobj = JSON.parse(res);
                    console.log(resobj.data)

                    if (resobj.code == "200") {

                      $.cookie('hop_account', account, { expires: 7 });
                     $.cookie('hop_user_id', resobj.data.user_id, { expires: 7 });
                      window.location.href = "home.html";
                    } else if (resobj.code == "201") {
                        console.log(resobj.msg);
                        alert("账号不存在或密码错误");
                    }

                }
            ).fail(function () {
                alert("服务器维护中");
            });


        } else {
            alert("不可为空或者长度大于5")
        }

    };
    var registBtn = document.getElementById("registBtn");
    registBtn.onclick = function () {
        var account = document.getElementById("account").value;
        var pwd = document.getElementById("pwd").value;
        if (account.length >= 4 && pwd.length >= 5) {
            $("#mymodal").modal("show");


        } else {
            alert("不可为空或者长度大于5")
        }
    };
    $(document).ready(function () {
        var registerCodeInput = $("#register_code");
        document.getElementById("mymodal").addEventListener('hide.bs.modal', event => {
            if (registerCodeInput.val() == "buma") {
                //regist
                var account = document.getElementById("account").value;
                var pwd = document.getElementById("pwd").value;
                $.post(
                    url,
                    {
                        "action": "addUser",
                        "account": account,
                        "pwd": pwd
                    },
                    function (res) {
                        console.log(res)
                        if (res == "0") {
                            console.log("register ok");
                            alert("注册成功，请点击登录");
                        } else if (res == "1") {
                            console.log("exist");
                            alert("注册失败，此账号已存在");
                        } else {
                            console.log("register error")
                            alert("注册错误");
                        }

                    }
                ).fail(function () {
                    alert("服务器维护中");
                });
            } else {
                alert("注册码填写错误")
            }
        })
    });


</script>

</html>